/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

package com.simplebible.bible.objects;

import java.util.ArrayList;
import java.util.List;
import com.simplebible.objects.CommonObject;
import wl.util.Utils;

/**
 *
 * @author Williams Lopez
 */
public class Abreviation extends CommonObject{

    public static int FIELD_BIBLE = 0;
    public static int FIELD_BOOK = 1;
    public static int FIELD_CHAPTER = 2;
    public static int FIELD_VERSE = 3;

    private String bible = null;
    private String book = null;
    private String chapter = null;
    private String verse = null;

    /**
     * Agregado ya que en algunas versiones
     * los versículos estan agrupados en uno solo
     */
    private List<Abreviation> aditionalReferences;

    public String getHumanName(){
        if(aditionalReferences == null || aditionalReferences.isEmpty()){
            return book + " "+chapter+":"+ verse;
        }

        StringBuilder sb = new StringBuilder();

        sb.append(book);
        sb.append(" ");
        sb.append(chapter);
        sb.append(":");
        sb.append(verse);

        if (aditionalReferences != null && !aditionalReferences.isEmpty()) {

            for (int i = 0; i < aditionalReferences.size(); i++) {
                Abreviation a = aditionalReferences.get(i);
                if (i == (aditionalReferences.size() - 1)) {
                    sb.append(" y ");
                } else {
                    sb.append(", ");
                }
                sb.append(a.getBook());
                sb.append(" ");
                sb.append(a.getChapter());
                sb.append(":");
                sb.append(a.getVerse());

            }
        }

        return sb.toString();
    }


    public Abreviation cloneChanging(int fieldToChange, String newValue){
        Abreviation newab = new Abreviation();
        if(fieldToChange==FIELD_BIBLE){
            newab.bible = newValue;
        }else{
            newab.bible = this.bible;
        }

        if(fieldToChange==FIELD_BOOK){
            newab.book = newValue;
        }else{
            newab.book = this.book;
        }

        if(fieldToChange==FIELD_CHAPTER){
            newab.chapter = newValue;
        }else{
            newab.chapter = this.chapter;
        }

        if(fieldToChange==FIELD_VERSE){
            newab.verse = newValue;
        }else{
            newab.verse = this.verse;
        }

        return newab;

    }


    public static Abreviation parse(String abreviations){

        String abreviation[] = abreviations.split("\\+");

        String data[] = abreviation[0].split("\\.");
        Abreviation ab = new Abreviation();

        if(data.length == 0){
            return null;
        }

        if(data.length >= 1){
            ab.book = data[0].trim();
            if(ab.book.equals("")){
                ab.book = null;
            }
        }

        if(data.length >= 2){
            if(Utils.isInteger(data[1])){
                ab.chapter = data[1];
            }else{
                ab.bible = data[1].trim();
                if(ab.bible.equals("")){
                    ab.bible = null;
                }
            }
        }

        if(data.length >= 3){
            if(Utils.isInteger(data[2])){
                ab.verse = data[2];
            }else{
                ab.bible = data[2].trim();
                if(ab.bible.equals("")){
                    ab.bible = null;
                }
            }
        }

        if(data.length >= 4){
            ab.bible = data[3].trim();
            if(ab.bible.equals("")){
                    ab.bible = null;
            }
        }

        if(ab.bible==null && ab.book == null && ab.chapter  == null && ab.verse == null){
            return null;
        }

        //agregando referencias adicionales
        if(abreviation.length > 1){
            ab.aditionalReferences = new ArrayList<Abreviation>();
            for(int i=1;i<abreviation.length;i++){
                ab.aditionalReferences.add(parse(abreviation[i]));
            }
        }

        return ab;
    }

    @Override
    public String toString(){

        StringBuilder sb = new StringBuilder();

        sb.append(book);

        if(chapter != null){
            sb.append(".");
            sb.append(chapter);
        }
        if(verse !=null ){
            sb.append(".");
            sb.append(verse);
        }

        if(bible !=null){
            sb.append(".");
            sb.append(bible);
        }

        if (aditionalReferences != null && !aditionalReferences.isEmpty()) {
            for (int i = 0; i < aditionalReferences.size(); i++) {
                Abreviation a = aditionalReferences.get(i);
                sb.append("+");
                sb.append(a.toString());
            }
        }

        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }

        if(this == obj){
            return true;
        }

        final Abreviation other = (Abreviation) obj;
        
//        boolean e = Utils.equalsOrNull(this.bible, other.bible)
//                    && Utils.equalsOrNull(this.book, other.book)
//                    && Utils.equalsOrNull(this.chapter, other.chapter)
//                    && Utils.equalsOrNull(this.verse, other.verse);

        boolean e = Utils.equalsOrNull(this.book, other.book)
                    && Utils.equalsOrNull(this.chapter, other.chapter)
                    && Utils.equalsOrNull(this.verse, other.verse);


        if (getAditionalReferences() == null || getAditionalReferences().isEmpty()) {
            return e;
        }

        for(Abreviation a : getAditionalReferences()){
            if(a.equals(other)){
                return true;
            }
        }

        return false;

    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + (this.bible != null ? this.bible.hashCode() : 0);
        hash = 79 * hash + (this.book != null ? this.book.hashCode() : 0);
        hash = 79 * hash + (this.chapter != null ? this.chapter.hashCode() : 0);
        hash = 79 * hash + (this.verse != null ? this.verse.hashCode() : 0);
        return hash;
    }





    public String getBook() {
        return book;
    }

    public void setBook(String book) {
        this.book = book;
    }

    public String getChapter() {
        return chapter;
    }

    public void setChapter(String chapter) {
        this.chapter = chapter;
    }

    public String getVerse() {
        return verse;
    }

    public void setVerse(String verse) {
        this.verse = verse;
    }

    public String getBible() {
        return bible;
    }

    public void setBible(String bible) {
        this.bible = bible;
    }

    public List<Abreviation> getAditionalReferences() {
        return aditionalReferences;
    }

    public void setAditionalReferences(List<Abreviation> aditionalReferences) {
        this.aditionalReferences = aditionalReferences;
    }

    



    


}
